// import type { AxiosRequestConfig } from "axios";
import $http from "./interceptors"
import { ElMessage } from "element-plus";

// 定义通用响应结构
// export interface ApiResponse<T> {
//     code: number;
//     data: T;
//     message: string;
//     total?: number;
// } 

// export interface IOptions {
//     useMessage?: boolean
//     useLoading?: boolean
// }

// export type extendAxiosRequestConfig = AxiosRequestConfig & IOptions   

const defaultOption = {
  useMessage: true,
  useLoading: true
}

// 封装 GET 请求
export const get = (url, params, options) => {
  const { useMessage } = options || {}
  return new Promise((resolve, reject) => {
    $http({
      url,
      method: 'get',
      params,
      ...defaultOption,
      ...options
    }).then((res) => {
      if (res.code === 200) {
        resolve(res)
      } else {
        if (useMessage) {
          ElMessage.error(res.message)
        }
        reject(res)
      }

    })
  })
}

// 封装 POST 请求
export const post = (url, params, options) => {
  const { useMessage } = options || {}
  return new Promise((resolve, reject) => {
    $http.post (url, params, { ...defaultOption, ...options }).then((res) => {
      if (options?.responseType === 'arraybuffer') {
        resolve(res)
        return
      }
      if (res.code === 200) {
        resolve(res)
      } else {
        if (useMessage) {
          ElMessage.error(res.message)
        }
        reject(res)
      }
    }).catch(err => {
      ElMessage.error(err.message)
    })
  })
}